package com.zhejiangzhengyuan.municipal_way.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhejiangzhengyuan.municipal_common.entity.InspectionRecord;
import com.zhejiangzhengyuan.municipal_common.entity.dto.InspectionRecordTaskDto;
import com.zhejiangzhengyuan.municipal_common.entity.dto.NewCase;
import com.zhejiangzhengyuan.municipal_common.entity.dto.UpdateFileDto;
import com.zhejiangzhengyuan.municipal_common.entity.vo.InspectionRecordQueryVo;
import com.zhejiangzhengyuan.municipal_common.entity.vo.InspectionTasksVo;
import com.zhejiangzhengyuan.municipal_common.entity.vo.TaskVo;
import com.zhejiangzhengyuan.municipal_common.entity.vo.UsernameVo;
import com.zhejiangzhengyuan.municipal_common.mapper.InspectionRecordMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.InspectionRecordQueryMapper;
import com.zhejiangzhengyuan.municipal_common.mapper.UserInfoMapper;
import com.zhejiangzhengyuan.municipal_common.response.ResponseVo;
import com.zhejiangzhengyuan.municipal_common.service.InspectionRecordService;
import com.zhejiangzhengyuan.municipal_common.utils.ConstantUtil;
import com.zhejiangzhengyuan.municipal_common.utils.excelFileUtils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author lin_ju
 * @since on 2019/12/4
 */
@Api(tags = "巡检记录管理")
@RestController
@RequestMapping("/way/inspectionRecord")
public class inspectionRecordController {
  @Autowired private InspectionRecordService inspectionRecordService;
  @Autowired private UserInfoMapper userInfoMapper;
  @Autowired private InspectionRecordMapper inspectionRecordMapper;
  @Autowired private InspectionRecordQueryMapper inspectionRecordQueryMapper;

  @ApiOperation("巡检记录查询")
  @GetMapping("/selectInspectionRecordManageVo")
  public ResponseVo selectInspectionRecordManageVo(
      @ApiParam("页码") Long page, @ApiParam("每页显示条数") Long limit, String status) {
    page = Objects.isNull(page) || page <= 0 ? 1 : page;
    limit = Objects.isNull(limit) || limit <= 0 ? 14 : limit;
    Page<InspectionTasksVo> inspectionRecordQueryVoPage = new Page<>(page, limit);
    IPage<InspectionTasksVo> inspectionRecordQueryVoIPage =
        inspectionRecordService.selectInspectionRecordManageVo(inspectionRecordQueryVoPage, status);
    return inspectionRecordQueryVoIPage == null
        ? ResponseVo.SERVER_ERROR
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, inspectionRecordQueryVoIPage);
  }

  @ApiOperation("查询单个")
  @GetMapping("/selectOneInspectionById")
  public ResponseVo selectOneInspectionById(Long id) {
    InspectionTasksVo inspectionTasksVo = inspectionRecordMapper.selectOneInspectionRecordById(id);
    return inspectionTasksVo == null
        ? ResponseVo.SERVER_ERROR
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, inspectionTasksVo);
  }

  @ApiOperation("新建巡检任务")
  @PostMapping("/insertInspectionRecordManage")
  public ResponseVo insertInspectionRecordManage(
      @ModelAttribute @Validated InspectionRecordTaskDto dto) {
    List<Long> longs = inspectionRecordService.insertInspectionRecordManage(dto);
    return longs == null
        ? ResponseVo.CLIENT_PARAM_ERROR
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, longs);
  }

  @ApiOperation("更新巡检文件")
  @PostMapping("/updateInspectionFile")
  public ResponseVo updateInspectionFile(@ModelAttribute UpdateFileDto updateFileDto) {
    int i = inspectionRecordService.updateInspectionFile(updateFileDto);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.CLIENT_PARAM_ERROR;
  }

  @ApiOperation("更新巡检任务")
  @PostMapping("/updateInspectionRecordManage")
  public ResponseVo updateInspectionRecordManage(
      @ModelAttribute InspectionRecordTaskDto inspectionRecordDto) {
    int i = inspectionRecordService.updateInspectionRecordManage(inspectionRecordDto);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.CLIENT_PARAM_ERROR;
  }

  @ApiOperation("根据ID 批量删除 格式：'ids':[7,9,10]")
  @DeleteMapping("/deleteInspectionRecordManage")
  public ResponseVo deleteInspectionRecordManage(@RequestBody Map<String, List<Long>> map) {
    List<Long> idList = map.get("ids");
    return inspectionRecordService.deleteInspectionRecordManage(idList)
        ? ResponseVo.SUCCESS
        : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("提交任务")
  @PostMapping("/commitInspectionRecordManage")
  public ResponseVo commitInspectionRecordManage(Long id) {
    int i = inspectionRecordService.commitInspectionRecordManage(id);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.CLIENT_PARAM_ERROR;
  }

  @ApiOperation("新建案件在未处理")
  @PostMapping("/insertInspectionCase")
  public ResponseVo insertInspectionCase(@Validated @ModelAttribute NewCase newCase) {
    return inspectionRecordService.insertInspectionCase(newCase) > 0
        ? ResponseVo.SUCCESS
        : ResponseVo.SERVER_ERROR;
  }

  @ApiOperation("接单")
  @PostMapping("/receivingTaskInspection")
  public ResponseVo receivingTaskInspection(Long id) {
    int i = inspectionRecordService.receivingTaskInspection(id);
    return i > 0 ? ResponseVo.SUCCESS : ResponseVo.CLIENT_PARAM_ERROR;
  }

  @ApiOperation("查询记录的上报数")
  @GetMapping("/selectInspectionRecord")
  public ResponseVo selectInspectionRecord() {
    List<InspectionRecordQueryVo> inspectionRecordQueryVos =
        inspectionRecordQueryMapper.queryInspectionRecord();
    for (InspectionRecordQueryVo inspectionRecordQueryVo : inspectionRecordQueryVos) {
      inspectionRecordQueryVo.setEventNumber(
          inspectionRecordQueryMapper.findIdByCount(inspectionRecordQueryVo.getId()));
    }
    return new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, inspectionRecordQueryVos);
  }

  @ApiOperation("新建巡检任务上报人id与姓名捆绑")
  @GetMapping("/usernameVo")
  public ResponseVo UsernameVo() {
    List<UsernameVo> usernameVos = userInfoMapper.usernameVo();
    return usernameVos == null
        ? ResponseVo.SERVER_ERROR
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, usernameVos);
  }

  @ApiOperation("导出数据")
  @GetMapping("/export")
  public void excelRoad(HttpServletResponse response) {
    List<InspectionTasksVo> inspectionTasksVos =
        inspectionRecordMapper.selectInspectionRecordManageVo();
    HashSet<String> exclude = new HashSet<>();
    exclude.add("createTime");
    exclude.add("updateTime");
    ExcelUtil.objectToExcelAndDownload(
        response, exclude, "巡检记录数据表", InspectionRecord.class, inspectionTasksVos);
  }

  @ApiOperation("模糊查询")
  @GetMapping("/inspectionRecordFuzzy")
  public ResponseVo inspectionRecordFuzzy(
      String location,
      String startTime,
      String endTime,
      @ApiParam("页码") Long page,
      @ApiParam("每页显示条数") Long limit)
      throws ParseException {
    page = Objects.isNull(page) || page <= 0 ? 1 : page;
    limit = Objects.isNull(limit) || limit <= 0 ? 14 : limit;
    Page<InspectionTasksVo> inspectionTasksVoPage = new Page<>(page, limit);
    Page<InspectionTasksVo> inspectionRecordFuzzy =
        inspectionRecordMapper.inspectionRecordFuzzy(
            inspectionTasksVoPage, location, startTime, endTime);
    return inspectionRecordFuzzy == null
        ? ResponseVo.SUCCESS
        : new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, inspectionRecordFuzzy);
  }

  @ApiOperation("查询全部任务")
  @GetMapping("/selectTaskAll")
  public ResponseVo selectTaskAll(String status) {
    List<TaskVo> taskVos = inspectionRecordService.selectTaskAll(status);
    return new ResponseVo<>(ConstantUtil.ResultCode.SUCCESS, taskVos);
  }

  // 时间格式设置
  @InitBinder
  public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
  }
}
